<?php
/**
 * @package 	mod_bt_contentshowcase - BT ContentShowcase Module
 * @version		2.0
 * @created		Oct 2011

 * @author		BowThemes
 * @email		support@bowthems.com
 * @website		http://bowthemes.com
 * @support		Forum - http://bowthemes.com/forum/
 * @copyright	Copyright (C) 2012 Bowthemes. All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 *
 */

// no direct access

defined('_JEXEC') or die('Restricted access');

$document = JFactory::getDocument();
$document->addStyleSheet(JURI::root() . "modules/mod_bt_contentshowcase/tmpl/css/accordion.btcontentshowcase.css");
$document->addStyleSheet(JURI::root() . "modules/mod_bt_contentshowcase/tmpl/css/jcarousel.css");
$itemsPerPages = $params->get( 'items_per_page', 5 );
$showArrow = $params->get( 'show_arrow', 1 );
$arrowPosition = $params->get('arrow_position', 'right');
$activateFirst = $params->get( 'activate_first', 1 );
$vertical = $params->get('slide_direction') != 'vertical' ? false : true;
//Get pages list array
$pages = null;
$totalPages = 0;
if($list) {
    $pages = array_chunk( $list, $itemsPerPages  );
    //Get total pages
    $totalPages = count($pages);
}


?>

<?php if(count($list)>0) : ?>

<div style="width:<?php echo $moduleWidthWrapper;?>">

	<div id="btcontentshowcase<?php echo $module->id; ?>" class="bt-contentshowcase<?php echo $moduleclass_sfx? ' bt-contentshowcase'.$params->get('moduleclass_sfx'):'';?>">
		
		<?php 
			$add_style = "";
			if( trim($params->get('content_title')) ){
			$add_style= "border: 1px solid #CFCFCF;padding:10px 0px;";
		?>
		<h3>
			<?php if($params->get('content_title_link')) {?>
                        <a href="<?php echo $params->get('content_title_link');?>"><span><?php echo $params->get('content_title') ?> </span></a>
                    <?php } else { ?>
                        <span><?php echo $params->get('content_title') ?> </span>                    
                    <?php   }?>
		</h3>
		<?php } ?>
		<div  style="width:<?php echo $moduleWidth.";".$add_style;?>">
                    <ul class="jcarousel jcarousel-skin-tango">
                    <?php foreach( $pages as $key => $list ): ?>
                        
                        <li>
                            <?php foreach( $list as $i => $row ): ?>

				<div class="bt-row <?php if($i==0) {echo 'bt-row-first'; if($activateFirst) echo ' actived';} else if($i==count($list)-1) echo ' bt-row-last';  ?>"  >

					<div class="bt-inner">
                                            
                                            <?php if( $showTitle ): ?>
						<a class="bt-title" target="<?php echo $openTarget; ?>"
							title="<?php echo $row->title; ?>"
							href="<?php echo $row->link;?>"
                                                > 
                                                    <?php echo $row->title_cut; ?> 
                                                    <?php if($showArrow){?>
                                                    <img src="<?php echo JURI::root() . 'modules/mod_bt_contentshowcase/tmpl/images/button.png' ?>" alt=""/>
                                                    <?php }?>
                                                </a>
							<?php endif; ?>
                                            <div class="bt-inner-wrapper">
                                            <?php if( $show_category_name ): ?>
                                            <?php if($show_category_name_as_link) : ?>
                                                    <a class="bt-category" target="<?php echo $openTarget; ?>"
                                                            title="<?php echo $row->category_title; ?>"
                                                            href="<?php echo $row->categoryLink;?>"> <?php echo $row->category_title; ?>
                                                    </a>
                                                    <?php else :?>
                                                    <span class="bt-category"> <?php echo $row->category_title; ?> </span>
                                                    <?php endif; ?>
                                                    <?php endif; ?>


                                                    <?php if( $row->thumbnail): ?>
                                                    
                                                    <div style="text-align:<?php echo $align_image?>">
                                                    <a target="<?php echo $openTarget; ?>"
                                                            class="bt-image-link"
                                                            title="<?php echo $row->title;?>" href="<?php echo $row->link;?>">
                                                            <img <?php echo $imgClass ?> src="<?php echo $row->thumbnail; ?>" alt="<?php echo $row->title?>"  style=" height:<?php echo $thumbHeight ;?>px; width:<?php echo $thumbWidth ;?>px; <?php echo ($align_image != 'center') ? 'float: '. $align_image : '';?>;" title="<?php echo $row->title?>" />
                                                    </a>
                                                    </div>
                                                    <?php endif ; ?>
                                                    <?php if( $showAuthor || $showDate ): ?>
                                                    <div class="bt-extra">
                                                    <?php if( $showAuthor ): ?>
                                                            <span class="bt-author"><?php 	echo JText::sprintf('BT_CREATEDBY' ,
                                                            JHtml::_('link',JRoute::_($row->authorLink),$row->author)); ?>
                                                            </span>
                                                            <?php endif; ?>
                                                            <?php if( $showDate ): ?>
                                                            <span class="bt-date"><?php echo JText::sprintf('BT_CREATEDON', $row->date); ?>
                                                            </span>
                                                            <?php endif; ?>
                                                    </div>
                                                    <?php endif; ?>

                                                    <?php if( $show_intro ): ?>
                                                    <div class="bt-introtext">
                                                    <?php echo $row->description; ?>
                                                    </div>
                                                    <?php endif; ?>

                                                    <?php if( $showReadmore ) : ?>
                                                    <p class="readmore">
                                                            <a target="<?php echo $openTarget; ?>"
                                                                    title="<?php echo $row->title;?>"
                                                                    href="<?php echo $row->link;?>"> <?php echo JText::_('READ_MORE');?>
                                                            </a>
                                                    </p>
                                                    <?php endif; ?>
                                            </div>
					</div>
					<!-- bt-inner -->
				</div>
				<!-- bt-row -->
				<?php endforeach; ?>
                                 
                        </li>
                    <?php endforeach; ?>    
                    </ul>    
		</div>
			<!-- bt-main-item page	-->
	</div>
	<!-- bt-container -->


</div>
			<?php else : ?>
<div>No result...</div>
			<?php endif; ?>
<div style="clear: both;"></div>
<?php
$nav_top = (-1)*(int)$params->get( 'navigation_top', 0 );
$nav_right = (-1)*(int)$params->get( 'navigation_right', 0 );
?>
<?php if( $totalPages  > 1 ): ?>

       
    <style>
        #btcontentshowcase<?php echo $module->id; ?> .jcarousel li{
                width: <?php echo $moduleWidth ?>;
                height: 1px;
                
        }
        <?php if(abs($nav_top) == 0 &&  trim($params->get('content_title') =="" )){ ?>
        #btcontentshowcase<?php echo $module->id; ?>{
                padding-top:32px;
        }
        <?php } ?>
        #btcontentshowcase<?php echo $module->id; ?> .jcarousel-next{
                top:<?php echo $nav_top - 35 ?>px!important;
                right:<?php echo $nav_right + 14 ?>px!important;
        }
        #btcontentshowcase<?php echo $module->id; ?> .jcarousel-prev{
                top:<?php echo $nav_top - 35 ?>px!important;
                right:<?php echo $nav_right + 30 ?>px!important;
        }
            
    </style>
    <script type="text/javascript">
        jQuery(document).ready(function(){
            //actived first
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .bt-row.actived').find('.bt-inner-wrapper').show(0, function(){
                    jQuery(this).prev().find('img').attr('src', '<?php echo JURI::root() . 'modules/mod_bt_contentshowcase/tmpl/images/button-active.png' ?>');
                }
            );
            // calculate height for jcarousel li
            <?php if(!$vertical){?>
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel li').each(function(){
                var self = jQuery(this);
                self.height(0);
                jQuery(this).find('.bt-row').each(function(){
                    
                    var height = jQuery(this).height() +
                                 parseInt(jQuery(this).css('margin-top')) +   
                                 parseInt(jQuery(this).css('margin-bottom')) +
                                 parseInt(jQuery(this).css('padding-top')) +
                                 parseInt(jQuery(this).css('padding-bottom'));
                    
                    self.height(self.height() + height);             
                });
            });
            <?php } else{ ?>
            var height = 0;
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel li').eq(0).find('.bt-row').each(function(){
                height += jQuery(this).height() +
                                 parseInt(jQuery(this).css('margin-top')) +   
                                 parseInt(jQuery(this).css('margin-bottom')) +
                                 parseInt(jQuery(this).css('padding-top')) +
                                 parseInt(jQuery(this).css('padding-bottom'));
           
            });
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel li').height(height);
            <?php } ?>
            //calculate height for jcoursel-clip
            
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').jcarousel({
                <?php if($params->get('pause_hover')){ ?>
                        initCallback: btContentShowcaseHoverCallback,
                <?php }?>
                itemLoadCallback : {
                    onBeforeAnimation: btContentShowcaseOnBeforAnimation,
                    onAfterAnimation: btContentShowcaseItemLoadCallback,
                },
                auto: <?php echo ($params->get('auto_start')) ? $params->get('interval', 5000) : '0' ?>,
                animation: <?php echo (int)$params->get('duration', '1000')?>,
                <?php if(!$params->get('next_back', 1)){?>
                    buttonNextHTML: null,
                    buttonPrevHTML: null,
                <?php }?>
                scroll : <?php echo $params->get('slide_item_per_time', 1) ?>,
                vertical: <?php echo ($vertical) ? 'true' : 'false' ?>,
                wrap : 'both'
                
            });
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel-clip').height(jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel li').eq(0).height());
        });
        <?php if($params->get('pause_hover')){ ?>
        function btContentShowcaseHoverCallback(showcase){
            showcase.clip.hover(function() {
                showcase.stopAuto();
            }, function() {
                showcase.startAuto();
            });
        }
        <?php } ?>
        function btContentShowcaseOnBeforAnimation(showcase, state){
            //alert('before');
            if(state != 'init'){
            var jCarouselClip = jQuery('#btcontentshowcase<?php echo $module->id; ?>').find('.jcarousel-clip');
            var jCarouselClipHeight = jCarouselClip.height();
            var btRowActived = jQuery('#btcontentshowcase<?php echo $module->id; ?> .bt-row.actived');
            var btInnerWrapper = btRowActived.find('.bt-inner-wrapper');
            var height = btInnerWrapper.height();
            if (height == null) height = 0;
            jCarouselClip.height(jCarouselClipHeight  - height);
            btInnerWrapper.parent().parent().parent().height(jCarouselClipHeight   - height);
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height(jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height()  - height);
            btRowActived.removeClass('actived');
            btInnerWrapper.hide();
            btInnerWrapper.prev().find('img').attr('src', '<?php echo JURI::root() . 'modules/mod_bt_contentshowcase/tmpl/images/button.png' ?>');
                            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height(jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height()  - height);
            
            
            }
            alert(state);
        }
        function btContentShowcaseItemLoadCallback(showcase, state){
            //alert('after');
//            var jCarousel = jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel');
//            var jCarouselClip = jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel-clip');
//            var index = showcase.first - 1;
//            var height = 0;
//            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel li').eq(index).find('.bt-row').each(function(){
//                height += jQuery(this).height() +
//                                 parseInt(jQuery(this).css('margin-top')) +   
//                                 parseInt(jQuery(this).css('margin-bottom')) +
//                                 parseInt(jQuery(this).css('padding-top')) +
//                                 parseInt(jQuery(this).css('padding-bottom'));
//                             
//            });
//            
//            if(height >0 ){
//                if(height < jCarouselClip.height()){
//                    //alert(jCarousel.height() - jCarouselClip.height() + height);
//                    //jCarousel.height(jCarousel.height() - jCarouselClip.height() + height);
//                }
//                //alert(height);
//                //jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height(jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height() + jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel-clip').height() - height);
//                jCarouselClip.animate({height: height}, 200);
//            }
        }
    </script>
<?php else: ?>
<script type="text/javascript">	
	(function(){
		jQuery('#btcontentshowcase<?php echo $module->id; ?>').fadeIn("fast");
	})();
</script>
<?php endif; ?>
<script type="text/javascript">	
        jQuery(document).ready(function(){
            
            
            var jCarouselClip = jQuery('#btcontentshowcase<?php echo $module->id; ?>').find('.jcarousel-clip');

            var inProgress = false;
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .bt-title').click(function(){
                if(inProgress){
                    return false;
                }
                inProgress = true;
                var jCarouselClipHeight = jCarouselClip.height();
                var decreaseHeight = 0;
                var increaseHeight = 0;
                if(!jQuery(this).parent().parent().hasClass('actived')){
                    decreaseHeight = jQuery(this).parent().parent().parent().find('.actived').find('.bt-inner-wrapper').height();
                    if (decreaseHeight == null) decreaseHeight = 0;
                    
                        
                    jQuery(this).parent().parent().parent().find('.actived')
                        .removeClass('actived').find('.bt-inner-wrapper').slideUp(300, function(){
                            jQuery(this).prev().find('img').attr('src', '<?php echo JURI::root() . 'modules/mod_bt_contentshowcase/tmpl/images/button.png' ?>');
                            
                        });
                    jQuery(this).parent().parent().addClass('actived');    
                    jQuery(this).next().slideDown(300, function(){
                        jQuery(this).prev().find('img').attr('src', '<?php echo JURI::root() . 'modules/mod_bt_contentshowcase/tmpl/images/button-active.png' ?>');
                        increaseHeight = jQuery(this).height();
                        if (increaseHeight == null) increaseHeight = 0;
                        jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height(jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height()   + increaseHeight - decreaseHeight);
                        jQuery(this).parent().parent().parent().height(jCarouselClipHeight  + increaseHeight - decreaseHeight);
                        jCarouselClip.animate({height: jCarouselClipHeight  + increaseHeight - decreaseHeight}, 200);
                        inProgress = false;
                    });
                }else{
                    var height = jQuery(this).parent().parent().find('.bt-inner-wrapper').height();
                    if (height == null) height = 0;
                    jQuery(this).parent().parent()
                        .removeClass('actived')
                        .find('.bt-inner-wrapper')
                        .slideUp(300, function(){
                            jQuery(this).prev().find('img').attr('src', '<?php echo JURI::root() . 'modules/mod_bt_contentshowcase/tmpl/images/button.png' ?>');
                            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height(jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').height()  - height);
                            jQuery(this).parent().parent().parent().height(jCarouselClipHeight   - height);
                            jCarouselClip.animate({height: jCarouselClipHeight  - height}, 200);
                            inProgress = false;
                        });
                }

                return false;
            });
        });
        </script>        
        <style type="text/css">
            <?php if($showArrow){?>
                #btcontentshowcase<?php echo $module->id; ?> .bt-title img{
			outline: none;
                        vertical-align: middle;
                        float: <?php echo $arrowPosition?>;
		}
                <?php }?>
                #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-inner-wrapper{
                    width: <?php echo ($moduleWidth - 20)?>px;
                    <?php if($params->get('item_height')){?>
                    height: <?php echo $params->get('item_height')?>px;
                    <?php }?>
                }
                
                <?php if($align_image != 'center'){?>
                #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-title-nointro,
                #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-introtext,
                #btcontentshowcase<?php echo $module->id; ?>  .bt-inner .bt-extra{
                    margin-<?php echo $align_image ?>: <?php echo ($thumbWidth + 10)?>px;
                }
                <?php
                }
                ?>
         </style>
         